package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author gyf
 * @date 2024-12-30
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //查询计划基本信息
        NursingPlan nursingPlan = getById(id);
        //批量查询关联信息
//        nursingProjectPlanMapper.selectList(new LambdaQueryWrapper<>().eq("plan_id", id));
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.getList(id);

        //封装数据
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        nursingPlanVo.setProjectPlans(list);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //插入计划基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        save(nursingPlan);

        //批量插入计划项目关联信息
        int i = nursingProjectPlanMapper.insertBatch(nursingPlanDto.getProjectPlans(), nursingPlan.getId());

        return i > 0 ? 1 : 0;

    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //先修改基础信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        updateById(nursingPlan);

        //在修改关联信息
        //判断关联信息集合是否为空
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(projectPlans != null && !projectPlans.isEmpty()){
            //根据计划id删除关联信息
            nursingProjectPlanMapper.deleteByPlanId(nursingPlanDto.getId());
            //添加关联信息
            nursingProjectPlanMapper.insertBatch(nursingPlanDto.getProjectPlans(), nursingPlanDto.getId());
        }

        return 1;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanByIds(Long[] ids)
    {
        //删除护理计划
        int i = nursingPlanMapper.deleteBatchIds(Arrays.asList(ids));

        //删除关联信息
        nursingProjectPlanMapper.deleteByPlanIds(ids);

        return i;

    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return nursingPlanMapper.deleteById(id);
    }
}
